//
//  LoginViewController.m
//  YYZB
//
//  Created by 顾悦 on 16/3/23.
//
//

#import "LoginViewController.h"

#import "ThemeColor.h"
#import "TextFieldBGView.h"

#import "NSString+MobileNo.h"

@interface LoginViewController ()

@property (weak, nonatomic) IBOutlet UIButton *toggleButton;

@property (weak, nonatomic) IBOutlet TextFieldBGView *usernameBGView;
@property (weak, nonatomic) IBOutlet TextFieldBGView *passwordBGView;
@property (weak, nonatomic) IBOutlet TextFieldBGView *verifyCodeBGView;
@property (weak, nonatomic) IBOutlet UILabel *tipLabel;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [ThemeColor lightGrayBGColor];
    self.title = @"登录";
    self.verifyCodeButton.enabled = NO;
    self.submitButton.enabled = NO;
    self.usernameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.verifyCodeTextField.keyboardType = self.usernameTextField.keyboardType = UIKeyboardTypeNumberPad;
    self.passwordTextField.secureTextEntry = YES;
    [self.usernameTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self.passwordTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self.verifyCodeTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)togglePasswordAndVerifyCode:(BOOL)flag {
    self.passwordBGView.hidden = self.verifyCodeButton.hidden = self.verifyCodeBGView.hidden = YES;
    self.tipLabel.hidden = !flag;
    if (!flag) {
        self.passwordBGView.hidden = NO;
        [self.toggleButton setTitle:@"手机动态验证码登录" forState:UIControlStateNormal];
    } else {
        self.verifyCodeButton.hidden = self.verifyCodeBGView.hidden = NO;
        [self.toggleButton setTitle:@"密码登录" forState:UIControlStateNormal];
    }
}

- (IBAction)togglePasswordAndVerifyCode {
    if (self.passwordBGView.hidden) {
        [self togglePasswordAndVerifyCode:NO];
    } else {
        [self togglePasswordAndVerifyCode:YES];
    }
    [self updateSubmitButtonStatus];
}

- (BOOL)shouldFetchVerifyCode {
    return [self.usernameTextField.text isMobileNumber];
}

- (void)updateVerifyCodeButtonStatus {
    if ([self shouldFetchVerifyCode]) {
        self.verifyCodeButton.enabled = YES;
    } else {
        self.verifyCodeButton.enabled = NO;
    }
}

- (BOOL)shouldSubmit {
    if ((![self isUsingVerifyCode]
         && [self.usernameTextField.text isMobileNumber]
         && self.passwordTextField.text.length > 0)
        || (self.verifyCodeTextField.text.length > 0
            && [self isUsingVerifyCode]
            && [self.usernameTextField.text isMobileNumber])) {
            return YES;
        }
    return NO;
}

- (void)updateSubmitButtonStatus {
    if ([self shouldSubmit]) {
        self.submitButton.enabled = YES;
    } else {
        self.submitButton.enabled = NO;
    }
}

- (void)textFieldDidChange:(UITextField *)textField {
    if (textField == self.usernameTextField) {
        [self updateVerifyCodeButtonStatus];
    }
    [self updateSubmitButtonStatus];
}

- (BOOL)isUsingVerifyCode {
    return !self.verifyCodeButton.hidden;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
